package cn.idietrade.idietradegoodsmodule8002.controller;

import cn.idietrade.common.pojo.SecondGoods;
import cn.idietrade.common.pojo.SecondUser;
import cn.idietrade.common.utils.EmptyUtils;
import cn.idietrade.common.utils.ReturnResultData;
import cn.idietrade.common.utils.SecondPage;
import cn.idietrade.idietradegoodsmodule8002.service.feignservice.ESGoodsFeignService;
import cn.idietrade.idietradegoodsmodule8002.service.goodsservice.SecondGoodsService;
import cn.idietrade.idietradegoodsmodule8002.utils.FastDFSClient;
import cn.idietrade.idietradegoodsmodule8002.utils.RabbitMQUtil;
import cn.idietrade.idietradegoodsmodule8002.utils.RedisUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;


/**
 * @author zjy
 * @version 1.0
 * @createTime 2022-11-30 15:02
 * @description
 */
@Slf4j
@RestController
public class SecondGoodsController {
    @Resource
    private SecondGoodsService secondGoodsService;


    @Resource
    private ESGoodsFeignService esGoodsFeignService;

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private FastDFSClient fastDFSClient;

    @Resource
    private RabbitMQUtil rabbitMQUtil;

    Logger logger = Logger.getLogger("SecondGoodsController");

    //上传缩略图片
    @RequestMapping("/uploadImg")
    public ReturnResultData uploadImageAndCrtThumbImage(MultipartFile file)throws Exception{
        ReturnResultData resultData =new ReturnResultData<>();
        String url = fastDFSClient.uploadImageAndCrtThumbImage(file);
        if(EmptyUtils.isNotEmpty(url)){
            resultData.setCode(1);
            resultData.setMessage("图片上传成功");
            resultData.setData(url);
        }else{
            resultData.setCode(0);
            resultData.setMessage("上传类型有误");
            resultData.setData(url);
            logger.info("----上传类型不正确----");
        }
        return resultData;
    }
    @RequestMapping("/deleteFile")
    public ReturnResultData deleteFile(String url){
        ReturnResultData resultData =new ReturnResultData();
        boolean flag = fastDFSClient.deleteFile(url);
        if(flag){
            resultData.setCode(1);
            resultData.setMessage("删除成功");
            resultData.setData(null);
        }else{
            resultData.setCode(0);
            resultData.setMessage("删除失败");
            resultData.setData(null);
        }
        return resultData;
    }


    //推荐商品展示+同城+搜索+分页
    @RequestMapping("/goodsShow")
    public ReturnResultData<SecondPage> goodsShow(@RequestParam(value = "token",required = false) String token, Integer pageNum, Integer pageSize, String keyword, String selectType, Integer goodsType){
        ReturnResultData<SecondPage> resultData=new ReturnResultData<>();
            try {
                SecondUser secondUser=null;
                if(token!=null) {
                    secondUser = JSON.parseObject(redisUtil.getStr(token), SecondUser.class);
                }
                 int cityId=0;
                 if(EmptyUtils.isNotEmpty(selectType)){ //证明选择同城商品
                     cityId=secondUser.getCityId();
                 }
                 resultData = esGoodsFeignService.goodsShow(pageNum, pageSize, keyword, cityId,goodsType);
                if (EmptyUtils.isNotEmpty(resultData)) {
                    resultData.setCode(1);
                    resultData.setMessage("推荐商品查询成功");
                    resultData.setData(resultData.getData());
                } else {
                    resultData.setCode(0);
                    resultData.setMessage("没有商品");
                    resultData.setData(null);
                }
            } catch (Exception e) {
                resultData.setCode(-1);
                resultData.setMessage("程序异常");
                resultData.setData(null);
                logger.info("推荐商品展示----程序异常");
                logger.info(e.getMessage());
                e.printStackTrace();
            }
        return resultData;
    }



    //展示关注用户的商品信息
    @RequestMapping("/queryConcerUserIdGoods")
    public ReturnResultData<PageInfo<SecondGoods>> queryConcerUserIdGoods(String token,Integer pageNum, Integer pageSize){
        ReturnResultData<PageInfo<SecondGoods>> resultData=new ReturnResultData<>();
            try {
                SecondUser secondUser = JSON.parseObject(redisUtil.getStr(token), SecondUser.class);
                int userId=secondUser.getId();
                PageInfo<SecondGoods> listPageInfo = secondGoodsService.queryConcerUserIdGoods(pageNum,pageSize,userId);
                if (EmptyUtils.isNotEmpty(listPageInfo)) {
                    resultData.setCode(1);
                    resultData.setMessage("关注用户商品查询成功");
                    resultData.setData(listPageInfo);
                } else {
                    resultData.setCode(0);
                    resultData.setMessage("没有关注用户商品");
                    resultData.setData(null);
                }
            } catch (Exception e) {
                resultData.setCode(-1);
                resultData.setMessage("程序异常");
                resultData.setData(null);
                logger.info("关注用户商品查询----程序异常");
                logger.info(e.getMessage());
                e.printStackTrace();
            }
        return resultData;
    }



    //发布商品
    @RequestMapping("/goodsRelease")
    public ReturnResultData goodsRelease(String token,String imgids, SecondGoods secondGoods){
        ReturnResultData resultData =new ReturnResultData<>();
            try {
                SecondUser secondUser = JSON.parseObject(redisUtil.getStr(token), SecondUser.class);
                secondGoods.setUserId(secondUser.getId());
                secondGoods.setCityId(secondUser.getCityId());
                int i = secondGoodsService.goodsRelease(imgids,secondGoods);
                if(i>0){
                    //向消息中间件投递消息
                    boolean sendMessage = rabbitMQUtil.sendMessage(JSON.toJSONString(secondGoods));
                    if(sendMessage){
                        resultData.setCode(1);
                        resultData.setMessage("商品发布成功");
                        resultData.setData(null);
                    }
                }else{
                    resultData.setCode(0);
                    resultData.setMessage("商品发布失败");
                    resultData.setData(null);
                }
            }catch (Exception e){
                resultData.setData(null);
                resultData.setCode(-1);
                resultData.setMessage("程序异常");
                logger.info("商品发布----程序异常");
                logger.info(e.getMessage());
                e.printStackTrace();
            }
        return resultData;
    }

    @RequestMapping("/queryGoodsAll")
    public List<SecondGoods> queryGoodsAll(@RequestParam(value = "keyword",required = false) String keyword,
                                           @RequestParam(value = "cityId",required = false) Integer cityId){
        List<SecondGoods> goodsList = null;
       try {
           goodsList=secondGoodsService.queryGoodsAll(keyword,cityId);
       }catch (Exception e){
           logger.info("所有商品查询----程序异常");
           logger.info(e.getMessage());
           e.printStackTrace();
       }
       return goodsList;
    }

    //根据商品id查询当前商品信息
    @RequestMapping("/queryOneGoods")
    public ReturnResultData<SecondGoods> queryOneGoods(Integer goodsId){
        ReturnResultData<SecondGoods> resultData = new ReturnResultData<>();
        try {
            SecondGoods secondGoods = secondGoodsService.queryOneGoods(goodsId);
            if(EmptyUtils.isNotEmpty(secondGoods)){
                resultData.setCode(1);
                resultData.setMessage("商品信息查询成功");
                resultData.setData(secondGoods);

            }else{
                resultData.setCode(0);
                resultData.setMessage("商品信息查询失败");
                resultData.setData(null);
            }
        }catch (Exception e){
            resultData.setData(null);
            resultData.setCode(-1);
            resultData.setMessage("程序异常");
            logger.info("商品信息查询----程序异常");
            logger.info(e.getMessage());
            e.printStackTrace();
        }
        return resultData;
    }


}
